import requests
from lxml import etree 
import re 

# 这个是请求的地址，但是该IP地址应该后台分配的，wlan_user_ip 因此通过访问得到其IP
# url = "http://172.30.255.42:801/eportal/portal/login?callback=dr1003&login_method=1&user_account=%2C0%2C435087&user_password=Asd123..&wlan_user_ip=172.29.38.113&wlan_user_ipv6=&wlan_user_mac=000000000000&wlan_ac_ip=&wlan_ac_name=&jsVersion=4.1.3&terminal_type=1&lang=zh-cn&v=6230&lang=zh"

headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
    "Host":"172.30.255.42"
    }

url = "http://172.30.255.42"
try:
    res2 = requests.get(url,headers=headers)

    res = re.compile(r"<script type=.*?>.*?v46ip='(?P<ip>.*?)'.*?",re.S)
    res1 = res.finditer(res2.text)
    # 替换后的校园网账号、密码
    username = ""
    password = ""
    ip = "172.17.14.69"
    for i in res1:
        ip = i.group("ip")
    url2 = f"http://172.30.255.42:801/eportal/portal/login?callback=dr1003&login_method=1&user_account=%2C0%2C{username}&user_password={password}&wlan_user_ip={ip}&wlan_user_ipv6=&wlan_user_mac=000000000000&wlan_ac_ip=&wlan_ac_name=&jsVersion=4.1.3&terminal_type=1&lang=zh-cn&v=6230&lang=zh"
    res3 = requests.get(url2,headers=headers)
    print(res3.text)
except:
    print("连接失败！")

# 初步实现了这些，还得考虑，如何判断网线连接状态、在合适的开机后运行（发生错误不报错即可）
# 而且要考虑连接wlan 的情况